package com.xxchat.controller;

import com.xxchat.annotation.GlobalInterceptor;
import com.xxchat.config.AppConfig;
import com.xxchat.entity.constants.QueryConstants;
import com.xxchat.entity.constants.SysConstants;
import com.xxchat.entity.enums.SysVersionFileTypeEnum;
import com.xxchat.entity.po.SysVersion;
import com.xxchat.entity.query.SysVersionQuery;
import com.xxchat.entity.vo.CheckVersionVO;
import com.xxchat.entity.vo.ResponseVO;
import com.xxchat.service.SysVersionService;
import com.xxchat.utils.CopyUtils;
import com.xxchat.utils.StringTools;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;

@RestController("sysVersionController")
@RequestMapping("/version")
public class SysVersionController extends ABaseController {
    @Resource
    private SysVersionService sysVersionService;

    @Resource
    private AppConfig appConfig;

    @RequestMapping("/checkVersion")
    public ResponseVO checkVersion(String version, String uid) {
        if (StringTools.isEmpty(version)) {
            return getSuccessResponseVO(null);
        }
        SysVersion sysVersion = this.sysVersionService.checkVersion(version, uid);
        if (null == sysVersion) {
            return getSuccessResponseVO(null);
        }
        CheckVersionVO checkVersionVO = CopyUtils.copy(sysVersion, CheckVersionVO.class);
        if (SysVersionFileTypeEnum.LOCAL.getType().equals(sysVersion.getFileType())) {
            File file = new File(appConfig.getProjectFolder() + SysConstants.FILE_FOLDER_PATH_FILE + sysVersion.getVersion() + SysConstants.VERSION_EXE_SUFFIX);
            checkVersionVO.setSize(file.length());
        }
        if (null != sysVersion.getUpdateDescArray()) {
            checkVersionVO.setVersionDesc(Arrays.asList(sysVersion.getUpdateDescArray()));
        }
        checkVersionVO.setFileName(SysConstants.SYSTEM_NAME + sysVersion.getVersion());
        return getSuccessResponseVO(checkVersionVO);
    }
}
